using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Text;

namespace FlashSolution
{
    /// <summary>
    /// http://code.google.com/p/swfobject/
    /// </summary>
	public class SWFObjectControl : System.Web.UI.Control
	{

        public enum enumFlashContainerTag
        {
            [StringValue("div")]
            Default,

            [StringValue("div")]
            Div,

            [StringValue("span")]
            Span,

            [StringValue("h1")]
            H1
        }


        public string ContainerCssClass
        {
            get;
            set;
        }

        public string FlashCssClass
        {
            get;
            set;
        }

        public string ximovie { get; set; }

        public enumFlashContainerTag FlashContainerTag
        {
            get;
            set;
        }

        private FlashAttributes _Attributes = new FlashAttributes();

        /// <summary>
        /// Attributes used to play the flash movie
        /// </summary>
        public FlashAttributes Attributes
        {
            get { return _Attributes; }
            set { _Attributes = value; }
        }

        #region AltHtml
        /// <summary>
        /// Overridable property to be used as the Default AltHtml when AltHtml=null or unset
        /// </summary>
        protected virtual string DefaultAltHtml
        {
            get
            {
                return string.Empty;
            }
        }

        /// <summary>
        /// Private AltHtml field
        /// </summary>
        private string _AltHtml = null;

        /// <summary>
        /// Alternate content when no flash-player is present
        /// </summary>
        public string AltHtml
        {
            get { return _AltHtml; }
            set { _AltHtml = value; }
        }
        #endregion



        protected override void CreateChildControls()
        {
            if (Attributes.movie == null) return;

            // Replacement Flash container
            string TagName = StringValueAttribute.StringFromEnum(FlashContainerTag);
            HtmlGenericControl SWFObjectContainer = new HtmlGenericControl(TagName);
            if (!string.IsNullOrEmpty(ContainerCssClass)) SWFObjectContainer.Attributes.Add("class", ContainerCssClass);

            // Set ID on container (needed!)
            SWFObjectContainer.ID = "SWFObjectContainer";

            // Add AltHtml (shown to users without a flashplayer installed)
            if (AltHtml != null)
            {
                SWFObjectContainer.InnerHtml = AltHtml;
            }
            else
            {
                SWFObjectContainer.InnerHtml = DefaultAltHtml;
            }
            this.Controls.Add(SWFObjectContainer);


            HtmlGenericControl SWFObjectScriptControl = new HtmlGenericControl("script");
            SWFObjectScriptControl.Attributes.Add("type", "text/javascript");
            SWFObjectScriptControl.Attributes.Add("language", "javascript");
            this.Controls.Add(SWFObjectScriptControl);

            #region Create parameters string
            System.Text.StringBuilder psb = new System.Text.StringBuilder();
            if (Attributes.play != FlashAttributes.enumBoolean.Default) AddParameter(psb, "play", StringValueAttribute.StringFromEnum(Attributes.play));
            if (Attributes.loop != FlashAttributes.enumBoolean.Default) AddParameter(psb, "loop", StringValueAttribute.StringFromEnum(Attributes.loop));
            if (Attributes.menu != FlashAttributes.enumBoolean.Default) AddParameter(psb, "menu", StringValueAttribute.StringFromEnum(Attributes.menu));
            if (Attributes.quality != FlashAttributes.enumQuality.Default) AddParameter(psb, "quality", StringValueAttribute.StringFromEnum(Attributes.quality));
            if (Attributes.scale != FlashAttributes.enumScale.Default) AddParameter(psb, "scale", StringValueAttribute.StringFromEnum(Attributes.scale));
            if (Attributes.salign != FlashAttributes.enumSAlign.Default) AddParameter(psb, "salign", StringValueAttribute.StringFromEnum(Attributes.salign));
            if (Attributes.wmode != FlashAttributes.enumWMode.Default) AddParameter(psb, "wmode", StringValueAttribute.StringFromEnum(Attributes.wmode));
            if (!string.IsNullOrEmpty( Attributes.bgcolor )) AddParameter(psb, "bgcolor", Attributes.bgcolor);
            if (!string.IsNullOrEmpty(Attributes.basepath )) AddParameter(psb, "base", Attributes.basepath);
            if (Attributes.swliveconnect != FlashAttributes.enumBoolean.Default) AddParameter(psb, "swliveconnect", StringValueAttribute.StringFromEnum(Attributes.swliveconnect));
            if (Attributes.devicefont != FlashAttributes.enumBoolean.Default) AddParameter(psb, "devicefont", StringValueAttribute.StringFromEnum(Attributes.devicefont));
            if (Attributes.allowscriptaccess != FlashAttributes.enumAllowScriptAccess.Default) AddParameter(psb, "allowscriptaccess", StringValueAttribute.StringFromEnum(Attributes.allowscriptaccess));
            if (Attributes.allowfullscreen != FlashAttributes.enumBoolean.Default) AddParameter(psb, "allowfullscreen", StringValueAttribute.StringFromEnum(Attributes.allowfullscreen));
            if (Attributes.seamlesstabbing != FlashAttributes.enumBoolean.Default) AddParameter(psb, "seamlesstabbing", StringValueAttribute.StringFromEnum(Attributes.seamlesstabbing));
            if (Attributes.allownetworking != FlashAttributes.enumNetworking.Default) AddParameter(psb, "allownetworking", StringValueAttribute.StringFromEnum(Attributes.allownetworking));
            
            psb.Insert(0,"var params = {");
            psb.Append("};");
            #endregion

            #region Create flashvars string
            StringBuilder fsb = new StringBuilder();

            foreach (string key in Attributes.flashvars.Keys)
            {
                string value = Attributes.flashvars[key];
                AddParameter(fsb, key, value);
            }

            fsb.Insert(0, "var flashvars = {");
            fsb.Append("};");
            #endregion

            #region Create attributes string
            StringBuilder asb = new StringBuilder();
            if (!string.IsNullOrEmpty(Attributes.id)) AddParameter(asb, "id", Attributes.id);
            if (!string.IsNullOrEmpty(Attributes.name)) AddParameter(asb, "name", Attributes.name);
            if (Attributes.align != FlashAttributes.enumAlign.Default) AddParameter(asb, "align", StringValueAttribute.StringFromEnum(Attributes.align));
            if (!string.IsNullOrEmpty(FlashCssClass)) AddParameter(asb, "styleclass", FlashCssClass);
            asb.Insert(0, "var attributes = {");
            asb.Append("};");
            #endregion

            System.Text.StringBuilder script = new System.Text.StringBuilder();
            script.Append(psb);
            script.Append(fsb);
            script.Append(asb);
            script.Append("swfobject.embedSWF(");
            script.Append("\"" + Attributes.movie + "\",");
            script.Append("\"" + SWFObjectContainer.ClientID + "\",");
            script.Append("\"" + Attributes.width + "\",");
            script.Append("\"" + Attributes.height + "\",");
            script.Append("\"" + Attributes.version + "\",");
            script.Append("\"" + ximovie + "\",flashvars,params,attributes);");
            SWFObjectScriptControl.Controls.Add(new LiteralControl(script.ToString()));
        }

        /// <summary>
        /// Helper method to add a parameter to a stringbuiler (param:"value", param:"value", ....)
        /// </summary>
        /// <param name="sb">Stringbuilder to which the parameter is appended</param>
        /// <param name="param">Name of the parameter</param>
        /// <param name="value">Value of the parameter</param>
        private void AddParameter(System.Text.StringBuilder sb, string param, string value)
        {
            if (sb.Length>0) sb.Append(", ");
            sb.AppendFormat("{0}:\"{1}\"" , param , HttpContext.Current.Server.UrlPathEncode(value) );
        }

    }
}
